package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.billview.config.aspect.PrintDataExtend;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.businessobject.util.PrintPreviewObj2Html;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class ConstructionLogViewPlugin implements IBillViewCustomPlugin {

    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private StorageFactory storageFactory;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    @Value("${print.perLineNum}")
    private int perLineNum;

    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {

        if(ArrayUtils.isEmpty(objects))
            return new Object[0];

        ConstructionLogVO[] constructionLogVOs = new ConstructionLogVO[objects.length];

        for (int i = 0; i <constructionLogVOs.length ; i++){
            constructionLogVOs[i] = (ConstructionLogVO)objects[i];
        }

        // 翻译施工单位
        enterpriseInfoQuery.tranEntInfoField(objects, new String[]{SiteInspectionVO.PK_CONSTRUCTIONUNIT},
                new String[]{SiteInspectionVO.CONSTRUCTION_UNIT_NAME});

        return objects;
    }

    @Override
    @PrintDataExtend
    public Map<String, Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {

        InspectItemVO[] inspectitems =  (InspectItemVO[]) BeanHelper.getProperty(superVO,"inspectitems");

        userInfoQuery.tranUserInfoField(new Object[]{superVO},new String[]{CommonFieldConst.CREATOR, CommonFieldConst.MANAGER},new String[]{"creator_name", CommonFieldConst.MANAGER_NAME});

        String pk_constructionunit = BeanHelper.getProperty(superVO,"pk_constructionunit")==null?"":(String)BeanHelper.getProperty(superVO,"pk_constructionunit");

        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                    .queryEnterpriseByIds(new String[]{pk_constructionunit});
        if(MapUtils.isNotEmpty(enterpriseInfoMap)){
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(pk_constructionunit);
            BeanHelper.setProperty(superVO,"constructionunit_name",enterpriseInfo==null?"":enterpriseInfo.getName());
        }

        PrintPreviewObj2Html obj2Html = new PrintPreviewObj2Html(PrintPreviewObj2Html.BASE_PREFIX, PrintPreviewObj2Html.IMG, perLineNum);

        if (inspectitems != null) {
            for (InspectItemVO inspectItemVO : inspectitems) {
                if (inspectItemVO.getPictures() != null) {
                    String picHtml = obj2Html.urls2Html(storageFactory.getStorageUtil().signUrlForMultiPictures(inspectItemVO.getPictures(), null, request));
                    inspectItemVO.setPicture_address(picHtml);
                }
            }
            BeanHelper.setProperty(superVO,"inspectitems",inspectitems);
        }

        Map<String,Object[]> resultMap = new HashMap<String,Object[]>();

        resultMap.put(BusinessObjectConsts.PM_CONSTRUCTIONLOG, new Object[]{superVO});
        resultMap.put(BusinessObjectConsts.SUB_PM_INSPECTITEM, inspectitems);

        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.CONSTRUCTIONLOG_BILLTYPE;
    }
}
